package mycl;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.io.IOUtils;

public class MyClassloader extends ClassLoader{

	@Override
	public Class<?> loadClass(String name) throws ClassNotFoundException {
		return super.loadClass(name);
	}
	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		ClassLoader parent = this.getParent();
		Class<?> c=null;
		if(parent!=null){
			try{
				c = parent.loadClass(name);
			}catch(ClassNotFoundException e){
				
			}
		}
		if(c==null){
			//使用自定义加载器进行加载
			ClassLoader clr = this.getClass().getClassLoader();
			Class<?> clz = clr.loadClass(name);
			URL url = clz.getResource(name.substring(name.lastIndexOf(".")+1,name.length())+".class");
			InputStream is;
			try {
				is = url.openConnection().getInputStream();
				byte[] bytes = IOUtils.toByteArray(is);
				 c = defineClass(name, bytes, 0,bytes.length);
			} catch (Exception e1) {
			}
		}
		return c;
	}
	




}
